<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->

<?php
include("../models/entities/class.AccountEntity.php");
@session_start();
if(isset($_SESSION["Username"]))
{
require_once("../models/webconfig.php");
require_once("../models/class.connection.php");
require_once("../models/entities/class.AccountEntity.php");
require_once("../models/repositories/class.AccountRepository.php");
require_once ("../models/entities/class.GroupEntity.php");

include("../models/repositories/class.GroupAuthorityRepository.php");
if($_SESSION["Username"] != $_GET["acc"])
{
    if(isset($_SESSION['Account'])){
        $groupAuthorityRepository = new GroupAuthorityRepository();
        $account = $_SESSION['Account'];
        if(!$groupAuthorityRepository->CheckExistGroupAuthority($account->GroupID_i, 1))
            header("location:NoAccess.php");
    }
    else
    {
        header("location:NoAccess.php");
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Sửa Tài Khoản</title>
        <link href="../content/Layout.css" rel="stylesheet" type="text/css" media="screen"/>
        <script type="text/javascript" src="../scripts/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="../scripts/adminaccount.js"></script>
        <script>
        function DoiMatKhau(){
        if(confirm("Bạn có muốn thay đổi mật khẩu"))
        {
            $("#tdPass").html("<input type=\"password\" style=\"width: 200px\" name=\"upPassword\" value=\"\" onchange=\"checkInfoUpdate()\"><label id=\"alertPassword\" style=\"color:red;\"></label>");
        }
        else
        {
            $("#tdPass").html("<a href=\"#\" onclick=\"DoiMatKhau()\" >Thay đổi mật khẩu</a>")
        }
    }
    </script>
    </head>
    <body>
        <div id="format">
            <div id="wrapper">
                <!--Ph?n main menu-->

                <?php
                include("../includes/backend_mainnav.php");
                $accres = new AccountRepository();
                $account = $accres->GetAccountByUserName($_GET["acc"]);

                ?>
                <center>
                    <div class="HeaderInsert"><h1>Sửa Tài Khoản</h1></div>
                    <?php
                                            if (isset($_POST["upUsername"])&& isset ($_POST["upPassword"])) {
                                                if ($_POST["upUsername"] != "" && $_POST["upPassword"] != "" && $_POST["upEmail"] != "" && $_POST["upYahooid"] != "") {
                                                    if (isset($_POST["upHoTro"])) {
                                                        $b = 1;
                                                    } else {
                                                        $b = 0;
                                                    }
                                                    $result = $accres->Update($_POST["upUsername"],md5($_POST["upPassword"]), $_POST["upEmail"], $_POST["upGroup"], $_POST["upYahooid"], $b);
                                                    if ($result) {
                                                        echo "Sửa tài khoản thành công";
                                                        $account=$accres->GetAccountByUserName($_POST["upUsername"]);
                                                    } else {
                                                        echo"Có lỗi khi sửa tài khoản" . mysql_error();
                                                    }
                                                } else {
                                                    echo "Nhập đầy đủ thông tin";
                                                }
                                            }
                                            if (isset($_POST["upUsername"])&& !isset ($_POST["upPassword"])) {
                                                if ($_POST["upUsername"] != "" && $_POST["upEmail"] != "" && $_POST["upYahooid"] != "") {
                                                    if (isset($_POST["upHoTro"])) {
                                                        $b = 1;
                                                    } else {
                                                        $b = 0;
                                                    }
                                                    $result = $accres->Update($_POST["upUsername"],$account->Password_s, $_POST["upEmail"], $_POST["upGroup"], $_POST["upYahooid"], $b);
                                                    if ($result) {
                                                        echo "Sửa tài khoản thành công";
                                                         $account=$accres->GetAccountByUserName($_POST["upUsername"]);
                                                    } else {
                                                        echo"Có lỗi khi sửa tài khoản" . mysql_error();
                                                    }
                                                } else {
                                                    echo "Nhập đầy đủ thông tin";
                                                }
                                            }

                                            ?>
                    <div class="ContentInsert">

                        <form action="../views/UpdateAccount.php?acc=<?php echo $account->Username_s; ?>" method="post" >
                            <table>
                                <tr>
                                    <td style="text-align: right">Tên Tài Khoản:</td>
                                    <td style="text-align: left;width: 270px;">
                                        <input type="text" style="width: 200px" name="upUsername" readonly="true" value="<?php echo $account->Username_s; ?>">
                                    </td>
                                    
                                </tr>
                                <tr>
                                    <td style="text-align: right">Mật Khẩu:</td>
                                    <td style="text-align: left;width: 270px;" id="tdPass">
<!--                                        <input type="password" style="width: 200px" name="upPassword" value=" echo $account->Password_s; ?>" onchange="checkInfoUpdate()">
                                        <label id="alertPassword" style="color:red;"></label>-->
                                        <a href="#" onclick="DoiMatKhau()" >Thay đổi mật khẩu</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="text-align: right">Email:</td>
                                    <td style="text-align: left;width: 270px;">
                                        <input type="text" style="width: 200px" name="upEmail" value="<?php echo $account->Email_s; ?>" onchange="checkInfoUpdate()">
                                        <label id="alertEmail" style="color:red;"></label>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="text-align: right">Group:</td>
                                    <td style="text-align: left;width: 270px;"><select name="upGroup">
                                            <?php
                                            $group = $accres->GetAllGroup();
                                            foreach ($group as $g) {
                                                if ($g->ID_i == $account->GroupID_i) {
                                            ?>
                                                    <option value="<?php echo $g->ID_i ?>" selected="selected"><?php echo $g->Name_s; ?></option>
                                            <?php } else {
 ?>
                                                    <option value="<?php echo $g->ID_i ?>"><?php echo $g->Name_s; ?></option>
                                            <?php
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="text-align: right">Yahoo_ID</td>
                                    <td style="text-align: left;width: 270px;">
                                        <input type="text" style="width: 200px" name="upYahooid" value="<?php echo $account->YahooID_s; ?>" onchange="checkInfoUpdate()" onfocus="checkInfoUpdate()">
                                        <label id="alertYahooid" style="color:red;"></label>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="text-align: right">Hỗ Trợ</td>
                                    <td style="text-align: left;width: 270px;"><?php
                                    if (ord($account->HoTro_b) == 1) {
                            ?>
                                        <input type="checkbox" name ="upHoTro" checked>
                                    <?php
                                    } else {
                                    ?>
                                        <input type="checkbox" name ="upHoTro">
                                    <?php
                                    }
                                    ?></td>
                                </tr>
                                <tr><td colspan="2" style="text-align: center"><td></tr>
                                        <tr>
                                            <td colspan="2" style="text-align: center" id="btnsubmit">
                                            <input type="submit" name ="inSubmit" value="Sửa Tài Khoản">
<!--                                                <input type="submit" name ="upSubmit" value="Sửa">-->
                                            </td>
                                        </tr>
                                    </table>
                                </form>
                            </div>
                        </center>
                    </div>
                </div>
<?php
                                            include("../includes/backend_footer.php");
?>
    </body>
</html>
<?php
}
else
{
    header("location:../views/Admin_login.php?logout=TRUE");
}
?>
